package net.zjitc.dao.impl;

import net.zjitc.dao.CartDao;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

/**
 * Created by Administrator on 2017/12/11.
 */
public class CartDaoImpl implements CartDao{

    public List<CartItem> findByUid(int uid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="SELECT * FROM cartitem c,product p WHERE p.pid=c.pid AND c.uid=? order by orderby desc";
        List<CartItem> cartItemList=new ArrayList<CartItem>();
        try {
            List<Map<String,Object>> list=runner.query(sql,new MapListHandler(),uid);
            for (Map<String,Object> m:list){
                Product product=new Product();
                CartItem cartItem=new CartItem();
                BeanUtils.populate(product,m);
                BeanUtils.populate(cartItem,m);
                cartItem.setProduct(product);
                //放到集合中去
                cartItemList.add(cartItem);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //判断集合中是否有元素，如果不为0，才有元素
        if (cartItemList.size()>0){
            return cartItemList;
        }
        return null;
    }

    public CartItem findByUidAndPid(int pid, int uid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from cartitem where pid=? and uid=?";
        CartItem cartItem=new CartItem();
        try {
            Map<String,Object> map=runner.query(sql,new MapHandler(),pid,uid);
            if (map==null){
                return null;
            }
            Product product=new Product();
            User user=new User();
            BeanUtils.populate(product,map);
            BeanUtils.populate(user,map);
            BeanUtils.populate(cartItem,map);
            cartItem.setProduct(product);
            cartItem.setUser(user);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cartItem;
    }

    public int add(CartItem cartItem) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into cartitem(cartid,pid,quantity,uid) values(?,?,?,?)";
        try {
            int count=runner.update(sql,cartItem.getCartid(),cartItem.getProduct().getPid(),cartItem.getQuantity(),cartItem.getUser().getUid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int update(CartItem ds_cartItem) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update cartitem set quantity=? where cartid=?";
        try {
            int count=runner.update(sql,ds_cartItem.getQuantity(),ds_cartItem.getCartid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteCart(String cartid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="delete from  cartitem where cartid=?";
        try {
            int count=runner.update(sql,cartid);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int clearCart(User user) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="delete from  cartitem where uid=?";
        try {
            int count=runner.update(sql,user.getUid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
